<?php
session_start(); // Initialize Session data
ob_start(); // Turn on output buffering
//session_name();
error_reporting(E_ALL);
ini_set('display_errors', '1');


if (isset($_SESSION["language"]))
$lang=$_SESSION["language"];

include "butterflyportal/ewcfg7.php";
include "butterflyportal/ewmysql7.php";
include "butterflyportal/phpfn7.php";
include "userfn.php";

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
header('Content-Type:text/html; charset=UTF-8');
header("Connection: Keep-Alive");
header("Keep-Alive: timeout=300");


include "lang_".$lang.".php";

global $conn, $Language;

		// Language object
		$Language = new cLanguage();


$debug=getParameter("debug");

		// Start timer
		$GLOBALS["gsTimer"] = new cTimer();

		// Open connection
		$conn = ew_Connect();

		global $agencyId;
		global $portalId;
		$portalId=getPortalId();
if ($portalId!=""){
		//$agencyId=getAgencyId();


		include "vars.php";
		if (getCurrentUserLevel()!=null &&  getCurrentUserLevel()<=0) {
			if (getParameter("mode")!=null)
			{
				$mode=getParameter("mode");
			}
		}

		//$mode="edit";

	/*	if (getCurrentUserLevel()==-1) {
			if (getParameter("mode")!=null) )
			{
				$mode=getParameter("mode");
			}
		}
		*/
/*

$sSql ="Select * From portal Where portal.agencyId = ".$agencyId;
Portal_log($sSql);
$rs=$GLOBALS["conn"]->Execute($sSql);
$colorOne=$rs->fields["OneColor"];
$colorTwo=$rs->fields["TwoColor"];
$colorThree=$rs->fields["ThreeColor"];
$logo=$rs->fields["sitelogo"];
$background=$rs->fields["background"];

*/

    //Get applications variables.
    getPortalAppVars();

    //get application locales
    $sSql="SELECT DISTINCT
portal_app_locale.value_$LANG as value,
portal_app_locale.`key`,
portal_app.`name`
FROM
portal_app_locale
INNER JOIN portal_portal_app ON portal_app_locale.appId = portal_portal_app.appId
INNER JOIN portal_app ON portal_portal_app.appId = portal_app.id
WHERE portal_portal_app.portalId=$portalId
";

    // echo $sSql;
    $rs=$GLOBALS["conn"]->Execute($sSql);  Portal_log($sSql);
    while ( !$rs->EOF)  {
        // echo $rs->fields["key"];
        eval("$"."var_".$rs->fields["name"]."_".$rs->fields["key"]."=\"".$rs->fields["value"]."\";");

        $rs->MoveNext();
    }


if (getParameter("app")!=null)
$portletName=getParameter("app");
else $portletName="";


if (getParameter("coupleId")!=null)
$coupleId=getParameter("coupleId");
else $coupleId="";


    if (getParameter("portletId")!=null)
        $portletId=getParameter("portletId");
    else $portletId=getObjByKey("portal_portletpage", "portletId", "id", $coupleId);;



$hasProperties=true;

    $portletID=$coupleId;

//echo $coupleId;
if ($coupleId!=""){ // get portlet preferences


    //initialize portlet preferences
    $sSql ="SELECT * FROM `portal_portlet_preferences_names` WHERE `portlet_id`=$portletId;";
   //	echo $sSql ;
    $rs=$GLOBALS["conn"]->Execute($sSql);
    
    $portletname=getObjByKey("portal_portlet","appname","id",$portletId);

    while ($rs && !$rs->EOF)  {

    
        eval("$"."portlet_".$portletname."_".$rs->fields["name"]."=null;");
        eval("$"."portlet_".$portletID."_".$rs->fields["name"]."=null;");
        $rs->MoveNext();
    }


	$portletUID="Portlet_".$coupleId."_";
$sSql ="SELECT * FROM portal_portlet_preferences WHERE `portlet_page_id`=".$coupleId.";";
	//echo $sSql ;
$rs=$GLOBALS["conn"]->Execute($sSql);


	 while (!$rs->EOF)  {

	 	$hasProperties=true;

	
	 eval("$"."portlet_".$portletname."_".$rs->fields["name"]."=\"".$rs->fields["value"]."\";");

         eval("$"."portlet_".$portletID."_".$rs->fields["name"]."=\"".$rs->fields["value"]."\";");


	$rs->MoveNext();
	}

}


//if (isset($portletname) && $portletname!="sharedblocks")
include "apps/".$portletName."/index.php";

if ($mode=="edit" && $hasProperties) {
?>
<script type="text/javascript">
function edit_portlet_config_<?php echo $coupleId; ?>()
		{



		$.get("/portlet.php",
		{app: "system",
		operation:"get_preferences",
		coupleId:<?php echo $coupleId; ?>},
		   function(data){

			$("#result_edit_<?php echo $coupleId; ?>").html(data);
				$("#result_edit_<?php echo $coupleId; ?>").dialog({
					autoOpen: true,
					height: 600,
					width: 800,
					modal: true,
					zIndex: 3999,
					buttons: {
						"Save": function() {
						save_portlet_config_<?php echo $coupleId; ?>();
						},
						"Reload Preferences": function() {
							reload_portlet_config_<?php echo $coupleId; ?>();
							},
						Cancel: function() {
							$( this ).dialog( "close" );
						}
					}


					});


		   });



			}



function export_portlet_config_<?php echo $coupleId; ?>()
		{



		$.get("/portlet.php",
		{app: "system",
		operation:"export_preferences",
		coupleId:<?php echo $coupleId; ?>},
		   function(data){

			$("#result_export_<?php echo $coupleId; ?>").html(data);
				$("#result_export_<?php echo $coupleId; ?>").dialog({
					autoOpen: true,
					height: 140 ,
					width: 600,
					modal: true,
					zIndex: 3999,
					buttons: {

					}



					});


		   });

			}

function import_portlet_config_<?php echo $coupleId; ?>()
		{



		$.get("/portlet.php",
		{app: "system",
		operation:"import_preferences",
		coupleId:<?php echo $coupleId; ?>},
		   function(data){

			$("#result_import_<?php echo $coupleId; ?>").html(data);
				$("#result_import_<?php echo $coupleId; ?>").dialog({
					autoOpen: true,
					height: 140 ,
					width: 600,
					modal: true,
					zIndex: 3999,
					buttons: {

					}


					});


		   });

			}

function credentials_config_<?php echo $coupleId; ?>(){

    $.get("/portlet.php",
        {app: "system",operation:"credentials_config",coupleId:<?php echo $coupleId; ?>},
        function(data){

            $("#result_credentials_config_<?php echo $coupleId; ?>").html(data);
            $("#result_credentials_config_<?php echo $coupleId; ?>").dialog({
                autoOpen: true,
                height: 340 ,
                width: 600,
                modal: true,
                zIndex: 3999,
                buttons: {
                    "Save": function() {
                        save_credentials_config_<?php echo $coupleId; ?>();
                    },
                    Cancel: function() {
                        $( this ).dialog( "close" );
                    }
                }
            });



        });
}

function save_credentials_config_<?php echo $coupleId; ?>(){
    $.get("/portlet.php",
        {app: "system",
            operation:"save_credentials_config",
            coupleId:<?php echo $coupleId; ?><?php

            $sSql ="SELECT * FROM portal_groups WHERE portalId=$portalId";
            //echo $sSql ;
            $rs=$GLOBALS["conn"]->Execute($sSql);
            while (!$rs->EOF)  {

                echo ",group_".$rs->fields["id"]." : getParameterValue_".$coupleId."('group_".$rs->fields["id"]."')";
                //echo ",".$rs->fields["name"]." :1";

                $rs->MoveNext();
            }

            ?>},
        function(data){

            if (data=="ok") {

                alert("Portlet credentials saved !.");



            }
            else  alert("There was an error, please contact your administrator !");


        });
}

function save_portlet_config_<?php echo $coupleId; ?>(){

	$.get("/portlet.php",
			{app: "system",
			operation:"save_preferences",
			coupleId:<?php echo $coupleId; ?><?php

					$sSql ="SELECT * FROM portal_portlet_preferences WHERE `portlet_page_id`=$coupleId;";
					//echo $sSql ;

					$rs=$GLOBALS["conn"]->Execute($sSql);
					while (!$rs->EOF)  {

						echo ",".$rs->fields["name"]." : getParameterValue_".$coupleId."('".$rs->fields["name"]."')";
						//echo ",".$rs->fields["name"]." :1";

						$rs->MoveNext();
						}

					?>},
			   function(data){
                   <?php unBlockUI(); ?>

			if (data=="ok") {

				//alert("Portlet preferences saved !.");
				//location.reload(true);

				edit_portlet_config_<?php echo $coupleId; ?>();

				}
			else  alert("There was an error, please contact your administrator !");


			   });

    <?php blockUI($var_portal_saving); ?>
}

function reload_portlet_config_<?php echo $coupleId; ?>(){

	$.get("/portlet.php",
			{app: "system",
			operation:"reload_preferences",
			coupleId:<?php echo $coupleId; ?>},
			   function(data){
                   <?php unBlockUI(); ?>
			if (data=="ok") {
				//alert("Portlet preferences reloaded !.");
				//	location.reload(true);
				edit_portlet_config_<?php echo $coupleId; ?>();
				}
			else  alert("There was an error, please contact your administrator !");


			   });

    <?php blockUI($var_portal_loading); ?>
}
function getParameterValue_<?php echo $coupleId; ?>(param)
{

    if ($("#"+param+"_<?php echo $coupleId; ?>").attr("type")=="checkbox")
    return $("#"+param+"_<?php echo $coupleId; ?>").attr("checked");
	//alert(param+" "+ $("#"+param+"_<?php echo $coupleId; ?>").val()+" "+$("#"+param+"_<?php echo $coupleId; ?>").attr("type"));
/*	$.each($("div[id^=result_]"), function() {
		  if (this.id!="result_<?php echo $coupleId; ?>") $(this).remove();
		});*/


	return $("#"+param+"_<?php echo $coupleId; ?>").val();
}
</script>
<div id="result_credentials_config_<?php echo $coupleId; ?>" title="Edit Portlet Credentials" ></div>
<div id="result_edit_<?php echo $coupleId; ?>" title="Edit Portlet Preferences" ></div>
<div id="result_import_<?php echo $coupleId; ?>" title="Import Portlet Preferences" ></div>
<div id="result_export_<?php echo $coupleId; ?>" title="Export Portlet Preferences" ></div>
<?php }

}else{
	echo "Portal Not Found !";
}?>